<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="注册类别">
              <!-- <j-search-select-tag v-model="queryParam.registrationCategory" dict="registration_category" placeholder="请选择注册类别"/> -->
              <a-select
                show-search
                v-model="registrationCategory"
                placeholder="请选择注册类别"
                style="width: 100%"
                allowClear
                :filterOption="filterOption"
                @change="handleCategoryChange"
              >
                <a-select-option v-for="d in categoryList" :key="d.id">
                  {{ d.registrationCategory }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="注册专业">
              <a-select
                show-search
                allowClear
                v-model="queryParam.registeredProfessional"
                placeholder="请选择注册专业"
                style="width: 100%"
                :filterOption="filterOption"
                @change="handleProfessionalChange"
              >
                <a-select-option v-for="d in professionalList" :key="d.registeredProfessional" :value="d.registeredProfessional">
                  {{ d.registeredProfessional }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="newSearchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdds({},'zclb')" type="primary" icon="plus">注册类别</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('物料类别表')" :disabled="isDisabledAuth('erpMaterialCategory:export')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import" :disabled="isDisabledAuth('erpMaterialCategory:import')">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" :disabled="isDisabledAuth('erpMaterialCategory:delete')"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:'100%'}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{getCheckboxProps:getCheckboxProps, selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        :indentSize="30"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-button
            class="ruleButton"
            :ghost="true"
            type="primary"
            icon="edit"
            @click="handleEdits(record)"
          />
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDeletes(record)">
            <a-button
              class="ruleButton"
              :ghost="true"
              type="primary"
              icon="delete"
            />
          </a-popconfirm>
          <template v-if="!record.parentId">
            <a-divider type="vertical"/>
            <a @click="handleAdds(record,'zczy')">添加注册专业</a>
          </template>
        </span>

      </a-table>
    </div>

    <ErpProfessionalManageModal ref="modalForm" @ok="modalFormOks"></ErpProfessionalManageModal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ErpProfessionalManageModal from './modules/ErpProfessionalManageModal'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import { getAction,deleteAction } from '@/api/manage'

  export default {
    name: 'ErpProfessionalManageList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpProfessionalManageModal,
    },
    data () {
      return {
        description: '专业类别管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        queryParam: {},
        // 列定义
        defColumns: [
          {
            title:'单据编号',
            dataIndex: 'billNo',
            width:200,
            ellipsis: true,
            customRender:(value, row, index) => {
              const obj = {
                children: value,
                attrs: {},
              };
              obj.attrs.align = 'left';//控制表体内容居左
              return obj;
            }
          },
          {
            title:'注册类别',
            width:400,
            dataIndex: 'registrationCategory',
            ellipsis: true,
            align:"center",
          },
          {
            title:'注册专业',
            align:"center",
            width:400,
            dataIndex: 'registeredProfessional',
            ellipsis: true,
          },
          {
            title:'备注',
            align:"center",
            width:400,
            dataIndex: 'remarks',
            ellipsis: true,
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:200,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
        url: {
          list: "/api/erp/files/professional/erpProfessionalCategory/list",
          delete: "/api/erp/files/professional/erpProfessionalCategory/delete",
          deleteBatch: "/api/erp/files/professional/erpProfessionalCategory/deleteBatch",
          exportXlsUrl: "/api/erp/files/professional/erpProfessionalCategory/exportXls",
          importExcelUrl: "api/erp/files/professional/erpProfessionalCategory/importExcel",
        },
        dictOptions:{},
        superFieldList:[],
        categoryList: [],
        professionalList: [],
        dataSource: [],
        registrationCategory: undefined
      }
    },
    created() {
      this.getCategoryData()
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleAdds (record,type) {
        this.$refs.modalForm.add(record,type)
        this.$refs.modalForm.title = type=='zclb'?'新增注册类别':'新增注册专业'
        this.$refs.modalForm.disableSubmit = false
      },
      handleEdits(record){
        let item = Object.assign({},record)
        if(record.parentId){
          item.registrationCategory = this.dataSource.filter(v => v.id == record.parentId)[0].registrationCategory
        }
        this.$refs.modalForm.edit(item,item.parentId?'zczy':'zclb')
        this.$refs.modalForm.title = item.parentId?'编辑注册专业':'编辑注册类别'
        this.$refs.modalForm.disableSubmit = false
      },
      newSearchReset(){
        this.queryParam = {}
        this.registrationCategory = undefined
        this.professionalList = []
        this.loadData(1)
      },
      handleDeletes (record) {
        if (!this.url.delete) {
          this.$message.error('请设置url.delete属性!')
          return
        }
        if (record.children&&record.children.length > 0) {
          this.$message.error('该类别下存在注册专业，不支持删除!')
          return
        }
        var that = this
        deleteAction(that.url.delete, {
          id: record.id
        }).then(res => {
          if (res.success) {
            that.$message.success(res.message)
            that.loadData()
            that.getCategoryData()
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      modalFormOks(){
        // 新增/修改 成功时，重载列表
        this.loadData();
        // 清空列表选中
        this.onClearSelected()
        this.getCategoryData()
      },
      handleCategoryChange(val){
        if(val){
          this.$set(this.queryParam,'registrationCategory',this.categoryList.filter(v => v.id==val)[0].registrationCategory)
          if(this.categoryList.filter(v => v.id==val).length&&this.categoryList.filter(v => v.id==val)[0].children){
            this.professionalList = this.categoryList.filter(v => v.id==val)[0].children
          } else {
            this.professionalList = []
          }
        } else {
          this.$set(this.queryParam,'registrationCategory',null)
          this.professionalList = []
        }
        this.$set(this.queryParam,'registeredProfessional',undefined)
        this.$set(this.queryParam,'parentId',undefined)
      },
      handleProfessionalChange(val){
        if(val){
          this.$set(this.queryParam,'parentId',this.professionalList.filter(v => v.registeredProfessional==val)[0].parentId)
          this.$set(this.queryParam,'registrationCategory',null)
        } else {
          this.$delete(this.queryParam,'parentId')
          if(this.registrationCategory){
            this.$set(this.queryParam,'registrationCategory',this.categoryList.filter(v => v.id==this.registrationCategory)[0].registrationCategory)
          }
        }
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getCategoryData(){
        var params = {
          pageNo: 1,
          pageSize: -1
        }
        getAction(this.url.list, params).then(res => {
          if (res.success) {
            this.categoryList = res.result.records || res.result
            
          } else {
            this.categoryList = [];
          }
        }).catch(err => {
          this.$message.warning(err.message)
        })
      },
      getCheckboxProps(record) {
        return {
          props: {
            disabled: !Object.keys(record).includes('children') || (record.children&&record.children.length>0)
          }
        }
      },
    }
  }
</script>
<style lang="less" scoped>
  @import '~@assets/less/common.less';
  /deep/.ant-table-thead > tr > th {
    text-align: center; 
  }
</style>
